package kevin.framework.basic.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import kevin.framework.basic.common.ui.AjaxResult;
import kevin.framework.basic.common.ui.ClientButton;
import kevin.framework.basic.domain.Menu;
import kevin.framework.basic.service.IBaseService;
import kevin.framework.basic.service.IMenuService;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends ResetfulBaseController<Menu, String> {

    IMenuService menuService;

    @Resource(name = "menuService")
    @Override
    public void setService(IBaseService<Menu, String> service) {
        this.service = service;
        menuService = (IMenuService)service;
    }

    /*
     * 页面 views/base/entityPath 文件夹路径
     * ***/
    @Override
    public String setPageModelPath(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return "base" + File.separator + entityPath;
    }

    @Override
    @RequestMapping(value = "/page/{pageName}", method = RequestMethod.GET)
    public String page(@PathVariable("pageName") String pageName, HttpServletRequest request) throws Exception {
        if ("tree".equals(pageName)) {
            String pid = request.getParameter("pid");
            JSONArray json = this.queryJson(pid, request);
            request.setAttribute("treeJson", json.toJSONString());
        }
        return super.page(pageName, request);
    }

    private JSONArray queryJson(String pid, HttpServletRequest request) {

        // DataGrid grid = getDataGridCfg(request);
        //
        // List<ClientButton> buttonlist = grid.getLineButton();

        List<ClientButton> buttonlist = new ArrayList<ClientButton>();
        buttonlist.add(ClientButton.getDefaultAddBtn());
        buttonlist.add(ClientButton.getDefaultDelBtn());
        buttonlist.add(ClientButton.getDefaultUpdateBtn());
        JSONArray json = new JSONArray();
        try {
            json = menuService.getMenuJsonByPid(pid, buttonlist);
        } catch (Exception e) {
            logger.error("", e);
        }
        return json;
    }

    /** 设置列表查询参数 **/
    @Override
    protected Map<String, Object> setListParams(HttpServletRequest request) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String pid = request.getParameter("pid");
        params.put("pid", pid);
        return params;
    }

    @Override
    @RequestMapping(value = "/query")
    public @ResponseBody AjaxResult query(HttpServletRequest request) throws Exception {
        String pid = request.getParameter("pid");
        JSONArray json = this.queryJson(pid, request);
        return returnSuccData(json);
    }

}
